﻿using System;
using System.Threading.Tasks;

using ApproveFlowEngine.Localization;
using ApproveFlowEngine.MultiTenancy;

using Microsoft.Extensions.Configuration;

using Volo.Abp.Account.Localization;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Identity.Blazor;
using Volo.Abp.SettingManagement.Blazor.Menus;
using Volo.Abp.TenantManagement.Blazor.Navigation;
using Volo.Abp.UI.Navigation;

namespace ApproveFlowEngine.Blazor.Menus;

public class ApproveFlowEngineMenuContributor : IMenuContributor
{
    #region Private Fields

    private readonly IConfiguration _configuration;

    #endregion Private Fields

    #region Public Constructors

    public ApproveFlowEngineMenuContributor(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    #endregion Public Constructors

    #region Public Methods

    public async Task ConfigureMenuAsync(MenuConfigurationContext context)
    {
        if (context.Menu.Name == StandardMenus.Main)
        {
            await ConfigureMainMenuAsync(context);
        }
        else if (context.Menu.Name == StandardMenus.User)
        {
            await ConfigureUserMenuAsync(context);
        }
    }

    #endregion Public Methods

    #region Private Methods

    private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
    {
        var administration = context.Menu.GetAdministration();
        var l = context.GetLocalizer<ApproveFlowEngineResource>();

        context.Menu.Items.Insert(
            0,
            new ApplicationMenuItem(
                ApproveFlowEngineMenus.Home,
                l["Menu:Home"],
                "/",
                icon: "fas fa-home",
                order: 0
            )

        )
            ;
        context.Menu.Items.Insert(
            1,
            new ApplicationMenuItem(
                    ApproveFlowEngineMenus.Approve,
                    l["Menu:Approve"],
                    "/approve",
                    icon: "fas fa-home",
                    order: 1
                ));

        context.Menu.Items.Insert(
            2,
                new ApplicationMenuItem(
                    "ApproveFlowEngineMenus.identity.roles",
                    l["Menu:identity:roles"],
                    "/identity/cusRoles",
                    icon: "fas fa-home",
                    order: 0
                )

            );
        context.Menu.Items.Insert(
        3,
            new ApplicationMenuItem(
                "ApproveFlowEngineMenus.identity.users",
                l["Menu:identity:users"],
                "/identity/cusUsers",
                icon: "fas fa-home",
                order: 0
            )

        );
        context.Menu.Items.Insert(
            3,
                new ApplicationMenuItem(
                    "ApproveFlowEngineMenus.identity.roles",
                    l["Menu:identity:roles"],
                    "/identity/roles",
                    icon: "fas fa-home",
                    order: 0
                )

            );
        context.Menu.Items.Insert(
        4,
            new ApplicationMenuItem(
                "ApproveFlowEngineMenus.identity.users",
                l["Menu:identity:users"],
                "/identity/users",
                icon: "fas fa-home",
                order: 0
            )

        );

        if (MultiTenancyConsts.IsEnabled)
        {
            administration.SetSubItemOrder(TenantManagementMenuNames.GroupName, 1);
        }
        else
        {
            administration.TryRemoveMenuItem(TenantManagementMenuNames.GroupName);
        }

        administration.SetSubItemOrder(IdentityMenuNames.GroupName, 2);
        administration.SetSubItemOrder(SettingManagementMenus.GroupName, 3);

        return Task.CompletedTask;
    }

    private Task ConfigureUserMenuAsync(MenuConfigurationContext context)
    {
        var l = context.GetLocalizer<ApproveFlowEngineResource>();
        var accountStringLocalizer = context.GetLocalizer<AccountResource>();
        var authServerUrl = _configuration["AuthServer:Authority"] ?? "";

        context.Menu.AddItem(new ApplicationMenuItem("Account.Manage", accountStringLocalizer["MyAccount"],
                $"{authServerUrl.EnsureEndsWith('/')}Account/Manage?returnUrl={_configuration["App:SelfUrl"]}", icon: "fa fa-cog", order: 1000, null, "_blank").RequireAuthenticated());
        context.Menu.AddItem(new ApplicationMenuItem("Account.Logout", l["Logout"], url: "~/Account/Logout", icon: "fa fa-power-off", order: int.MaxValue - 1000).RequireAuthenticated());

        return Task.CompletedTask;
    }

    #endregion Private Methods
}